package com.fone.player.play.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.fone.player.R;
import com.fone.player.activity.BaseFragment;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.LinkvideoRst;
import com.fone.player.client.LinkvideoRst.Rcmd;
import com.fone.player.client.Request;
import com.fone.player.constant.FoneConstant;
import com.fone.player.play.FonePlayerActivity;
import com.fone.player.play.LinkVideoEvent;
import com.fone.player.play.LinkVideoEvent.LinkVideoFrom;
import com.fone.player.play.VideoPlayerMessage;
import com.fone.player.play.adapter.VideoRelatedAdapter;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.view.ColorBallProgressView;
import com.fone.player.view.LegalNoticeDialog;

import de.greenrobot.event.EventBus;

/**
 * 视频相关
 * 
 * @author kongxiaojun
 * @since 2014-4-30
 */
@SuppressLint("ValidFragment")
public class VideoRelatedFragment extends BaseFragment {

	/** 内容 */
	private View contentView;
	/** 列表 */
	private ListView rList;
	private String linkUrl;
	private VideoRelatedAdapter adapter;
	private Button btLaw;
	private LinkvideoRst linkvideoRst;
	private ColorBallProgressView relatedLoading;
	private static final String TAG = "VideoRelatedFragment";

	public static final Fragment newInstances(String linkUrl) {
		Fragment fragment = new VideoRelatedFragment();
		Bundle bundle = new Bundle();
		bundle.putString("linkUrl", linkUrl);
		fragment.setArguments(bundle);
		return fragment;
	}

	public VideoRelatedFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		contentView = inflater.inflate(R.layout.video_related, null);
		this.linkUrl = FoneUtil.handleUrl(getArguments().getString("linkUrl"));
		rList = (ListView) contentView.findViewById(R.id.video_related_list);
		btLaw = (Button) contentView.findViewById(R.id.video_related_law);
		relatedLoading = (ColorBallProgressView) contentView.findViewById(R.id.related_loading);
		adapter = new VideoRelatedAdapter();
		rList.setAdapter(adapter);
		rList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (getActivity() != null) {
					FonePlayerActivity activity = (FonePlayerActivity) getActivity();
					if (activity.isBuffering()) {
						return;
					}
					if (FoneUtil.isNetOkWithToast(getActivity())) {

						Rcmd rcmd = (Rcmd) adapter.getItem(arg2);
						// // 点击打开视频
						// Intent intent = new Intent();
						// intent.putExtra("from", PlayerFrom.LINKVIDEO.name());
						// intent.putExtra("action",
						// PlayerAction.DETAIL.name());
						// intent.putExtra("name", rcmd.name);
						// intent.putExtra("vgdetail", rcmd.vturl);
						// if (getActivity() instanceof FonePlayerActivity) {
						// ((FonePlayerActivity)
						// getActivity()).handNewIntent(intent);
						// }
						VideoPlayerMessage msg = new VideoPlayerMessage();
						msg.what = VideoPlayerMessage.MESSGAE_OPEN_RELEATED;
						msg.obj = new LinkVideoEvent(rcmd.vturl, rcmd.url, LinkVideoFrom.DETAILLINK, rcmd.name);
						EventBus.getDefault().post(msg);
					}
				}
			}
		});
		btLaw.setOnClickListener(new View.OnClickListener() {

			private long lastClickTime;

			@Override
			public void onClick(View v) {
				if (System.currentTimeMillis() - lastClickTime > 500) {
					LegalNoticeDialog lawDialog = new LegalNoticeDialog(getActivity(), FoneUtil.getDeclaration(getActivity(), FoneConstant.DETAIL_LEGAL_NOTICE));
				}
				lastClickTime = System.currentTimeMillis();
			}
		});
		if (FoneUtil.isNetOk(getActivity()) && linkvideoRst == null) {
			requestRelatedVideo();
		} else if (linkvideoRst != null) {
			adapter.setData(linkvideoRst.shost, linkvideoRst.rcmds.rcmdList);
		}
		return contentView;
	}

	private void requestRelatedVideo() {
		relatedLoading.setVisibility(View.VISIBLE);
		linkUrl = FoneUtil.handleUrl(linkUrl);
		Request.getInstance().linkvideo(linkUrl, new Callback<LinkvideoRst>() {
			@Override
			public void onSuccess(LinkvideoRst t) {
				if (destory) {
					return;
				}
				relatedLoading.setVisibility(View.GONE);
				if (t == null) {
					return;
				}
				if (t.result == 0) {
					linkvideoRst = t;
					// 请求成功
					adapter.setData(t.shost, t.rcmds.rcmdList);
				} else {
					// 请求失败
					if (t.error != null && !TextUtils.isEmpty(t.error.errormsg)) {
						L.v(TAG, "requestRelatedVideo error : " + t.error.errormsg);
					}
				}
			}

			@Override
			public void onFailure(Error error) {
				if (destory) {
					return;
				}
				relatedLoading.setVisibility(View.GONE);
				L.v(TAG, "requestRelatedVideo onFailure " + error.getReason());
				// if (error.isNetworkError()) {
				// if (getActivity() != null) {
				// Toast.makeText(getActivity(), R.string.network_error,
				// Toast.LENGTH_SHORT).show();
				// }
				// } else {
				// if (getActivity() != null) {
				// Toast.makeText(getActivity(), error.getReason(),
				// Toast.LENGTH_SHORT).show();
				// }
				// }
			}
		});
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		contentView = null;
		if (rList != null) {
			rList.removeAllViewsInLayout();
			rList = null;
		}
		adapter = null;
		btLaw = null;
		linkvideoRst = null;
		relatedLoading = null;
	}

}
